limsReport.searchPage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-report-search-list-grid',
            url: limsReport.baseUrl + '/queries',
            requestData: {
                processStatus_EQ: 'done'
            },
            columns: limsReport.getSearchListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.getAsyncComp('lims-report-uploader-search').done(function (grid) {
                        grid.setOptions({
                            bizId: -1
                        })
                    });
                }
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('lims-report-uploader-search').done(function (grid) {
                    grid.setOptions({
                        bizId: row.id
                    })
                });
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.REPORT.ONLINE.VIEW',
                icon: 'preview',
                onClick: function () {
                    var grid = Gikam.getComp('lims-report-search-list-grid');
                    var selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    window.open(limsReport.baseUrl + '/preview-online/reports' + Gikam.param({
                        ids: selections.map(function (item) {
                            return item.id;
                        }).join(',')
                    }));
                }
            }, {
                type: 'button',
                text: 'GIKAM.TITLE.REPORT.DOWNLOAD',
                icon: 'export',
                onClick: function () {
                    limsReport.downloadReport('lims-report-search-list-grid');
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.UNDO',
                icon: 'disable',
                onClick: function () {
                    _this.undo()
                }
            }]
        }
    },

    undo: function () {
        var grid = Gikam.getComp('lims-report-search-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return
        }
        Gikam.createLimsModal({
            title: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
            width: 500,
            height: 250,
            onConfirm: function () {
                var form = Gikam.getComp('comments-form')
                if (form.validate()) {
                    workspace.window.showMask()
                    var data = form.getData()
                    let url = limsReport.baseUrl + '/action/undo'
                    Gikam.put(url, Gikam.getJsonWrapper(data, [null, selections])).done(req => {
                        grid.refresh()
                    }).always(function () {
                        workspace.window.closeMask();
                    })
                    Gikam.getLastModal().close(data);
                }
            },
            m: {
                type: 'form',
                id: 'comments-form',
                columns: 2,
                titleWidth: 100,
                fields: [{
                    field: 'bpmn_comment',
                    title: 'GIKAM.WORKFLOW.SUBMIT.OPINION.TITLE',
                    type: 'textarea',
                    colspan: 2,
                    height: 100
                }],
                onRendered: function () {
                    this.setData({
                        bpmn_comment: '召回'
                    })
                }
            },
        })
    },

    getBaseInfoTab: function () {
        var _this = this;
        return {
            type: 'tab',
            panels: [{
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'lims-report-uploader-search-audit',
                    dbTable: 'T_LIMS_REPORT',
                    readonly: true
                }]
            }]
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-report-search-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '60%',
                items: [this.getGridParam()]
            },
            center: {
                items: [this.getBaseInfoTab()]
            }
        });
    },

    init: function () {
        this.create();
    }
};
